/*************************************
 * Created by WebStorm
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time :2024/2/5 18:22
 Desc :权限指令
 **************************************/
import {ObjectDirective, DirectiveBinding} from 'vue';
import {isArray, isObject} from "@/util/is.ts";
import {permsJudg} from '@/util/index.ts'
import {UseAppStore} from '@/stores/app.ts';

export const permission: ObjectDirective = {
    mounted(el: HTMLButtonElement, binding: DirectiveBinding<any>) {
        const appStore = UseAppStore();
        if (binding.value == undefined) return;
        let value: string;
        let isDisabled: boolean = false;
        if (isArray(binding.value) || isObject(binding.value)) {
            if (isObject(binding.value)) {
                value = binding.value['action'];
                isDisabled = binding.value['effect'] != undefined && binding.value['effect'] == 'disabled';
            } else {
                value = binding.value[0];
            }
        } else {
            value = binding.value;
        }
        if (!permsJudg(value)) {
            if (appStore.permissionDisabled) {
                el.disabled = true;
                el.style['disabled'] = 'disabled';
                el.classList.add('n-button--disabled');
            } else {
                if (isDisabled) {
                    el.disabled = true;
                    el.style['disabled'] = 'disabled';
                    el.classList.add('n-button--disabled');
                } else {
                    // @ts-ignore
                    el.parentNode.removeChild(el)
                }
            }
        }
    }
};